{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "f1d2c929",
   "metadata": {},
   "outputs": [],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "import pandas as pd\n",
    "import pandas_datareader.data as web\n",
    "import requests\n",
    "import bs4 as bs\n",
    "import schedule\n",
    "import time"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "39d0e365",
   "metadata": {},
   "outputs": [],
   "source": [
    "def get_stock_code():\n",
    "    res = requests.get('https://www.banban.cn/gupiao/list_sz.html')\n",
    "    res.encoding = res.apparent_encoding\n",
    "    soup = bs.BeautifulSoup(res.text, 'lxml')\n",
    "    content = soup.find('div', {'class':'u-postcontent cz'})\n",
    "    result = []\n",
    "    for item in content.findAll('a'):\n",
    "        result.append(item.text)\n",
    "    return result\n",
    "\n",
    "def get_stock_history(stock_name, stock_code, save_dir):\n",
    "    if stock_code[-2:] != 'sz':\n",
    "        stock_code = str(stock_code) + '.sz'\n",
    "    df = web.DataReader(stock_code, data_source='yahoo', start='2000-01-01', end='2021-10-28')\n",
    "    df.to_csv(save_dir + '/{}{}.csv'.format(stock_name, stock_code))\n",
    "\n",
    "if __name__=='__main__':\n",
    "    stock_name_code = get_stock_code()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b44e8135",
   "metadata": {},
   "outputs": [],
   "source": [
    "stock_name_code.remove('*ST斯太(000760)')\n",
    "stock_name_code.remove('*ST凯迪(000939)')\n",
    "stock_name_code.remove('安泰科技(000969)')\n",
    "stock_name_code.remove('ST天宝(002220)')\n",
    "stock_name_code.remove('天夏智慧(000662)')\n",
    "stock_name_code.remove('*ST德奥(002260)')\n",
    "stock_name_code.remove('*ST北讯(002359)')\n",
    "stock_name_code.remove('*ST康得(002450)')\n",
    "stock_name_code.remove('天广中茂(002509)')\n",
    "stock_name_code.remove('*ST龙力(002604)')\n",
    "stock_name_code.remove('*ST欧浦(002711)')\n",
    "stock_name_code.remove('长城影视(002071)')\n",
    "stock_name_code.remove('南岭民爆(002096)')\n",
    "stock_name_code.remove('运机集团(001288)')\n",
    "stock_name_code.remove('中航善达(000043)')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "bf1d9a83",
   "metadata": {},
   "outputs": [],
   "source": [
    "date = input('Date you want to update(2021-10-28):')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "613c32bd",
   "metadata": {},
   "outputs": [],
   "source": [
    "start_date = date\n",
    "end_date = date\n",
    "def get_stock_update(stock_name, stock_code, save_dir, save_name, start_date, end_date):\n",
    "    if stock_code[-2:] != 'sz':\n",
    "        stock_code = str(stock_code) + '.sz'\n",
    "    h = web.DataReader(stock_code, data_source='yahoo', start = start_date, end = end_date)\n",
    "    h = pd.DataFrame(h)\n",
    "    n = pd.read_csv(save_name, index_col = 'Date', parse_dates=True)\n",
    "    n = pd.concat([n, h])\n",
    "    print(n)\n",
    "    n.to_csv(save_dir + '/{}{}.csv'.format(stock_name, stock_code))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "05642eb2",
   "metadata": {},
   "outputs": [],
   "source": [
    "def job1():\n",
    "    if __name__=='__main__':\n",
    "        stock_name_code = get_stock_code()\n",
    "        for i in stock_name_code[1:500]:\n",
    "            stock_name = i[:-8]\n",
    "            stock_code = i[-7:-1]\n",
    "            stock_name_c = i[:-8]+i[-7:-1]+'.sz.csv'\n",
    "            get_stock_update(stock_name, stock_code, '/Users/zhangyuwei/Downloads/stock sz', stock_name_c, start_date, end_date)\n",
    "def job2():\n",
    "    if __name__=='__main__':\n",
    "        stock_name_code = get_stock_code()\n",
    "        for i in stock_name_code[501:1000]:\n",
    "            stock_name = i[:-8]\n",
    "            stock_code = i[-7:-1]\n",
    "            stock_name_c = i[:-8]+i[-7:-1]+'.ss.csv'\n",
    "            get_stock_update(stock_name, stock_code, '/Users/zhangyuwei/Downloads/stock sz', stock_name_c, start_date, end_date)\n",
    "def job3():\n",
    "    if __name__=='__main__':\n",
    "        stock_name_code = get_stock_code()\n",
    "        for i in stock_name_code[1001:1500]:\n",
    "            stock_name = i[:-8]\n",
    "            stock_code = i[-7:-1]\n",
    "            stock_name_c = i[:-8]+i[-7:-1]+'.sz.csv'\n",
    "            get_stock_update(stock_name, stock_code, '/Users/zhangyuwei/Downloads/stock sz', stock_name_c, start_date, end_date)\n",
    "def job4():\n",
    "    if __name__=='__main__':\n",
    "        stock_name_code = get_stock_code()\n",
    "        for i in stock_name_code[1501:]:\n",
    "            stock_name = i[:-8]\n",
    "            stock_code = i[-7:-1]\n",
    "            stock_name_c = i[:-8]+i[-7:-1]+'.sz.csv'\n",
    "            get_stock_update(stock_name, stock_code, '/Users/zhangyuwei/Downloads/stock sz', stock_name_c, start_date, end_date)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a15576df",
   "metadata": {},
   "outputs": [],
   "source": [
    "timer = input('time? ')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b1719814",
   "metadata": {},
   "outputs": [],
   "source": [
    "schedule.every().day.at(timer).do(job1)\n",
    "schedule.every().day.at(timer).do(job2)\n",
    "schedule.every().day.at(timer).do(job3)\n",
    "schedule.every().day.at(timer).do(job4)\n",
    "while True:\n",
    "    schedule.run_pending()\n",
    "    time.sleep(1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "27668a5f",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
